/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.acl.server;

import java.util.List;

import nl.biz_e.acl.client.UserService;
import nl.biz_e.acl.server.dao.MembershipDao;
import nl.biz_e.acl.server.dao.UserDao;
import nl.biz_e.acl.server.dao.UserGroupDao;
import nl.biz_e.acl.shared.model.Authorization;
import nl.biz_e.acl.shared.model.User;
import nl.biz_e.acl.shared.model.UserGroup;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

/**
 * Remote (server side) implementation of the {@link UserService} interface
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class UserServiceImpl extends RemoteServiceServlet implements
        UserService {
    private static final long serialVersionUID = 1L;
    private static final AuthorizationServiceImpl authService = new AuthorizationServiceImpl();
    private static final UserDao userDao = new UserDao();
    private static final UserGroupDao groupDao = new UserGroupDao();
    private static final MembershipDao membershipDao = new MembershipDao();

    /**
     * {@inheritDoc}
     */
    @Override
    public User saveUser(User user) {
        userDao.save(user);
        return user;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public UserGroup saveUserGroup(UserGroup group) {
        groupDao.save(group);
        return group;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deleteMembership(User user, UserGroup group) {
        membershipDao.delete(user, group);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deleteUser(User user) {
        List<Authorization> authorizations = authService
                .getAuthorizations(user);
        for (Authorization authorization : authorizations) {
            authService.deleteAuthorization(authorization);
        }
        List<UserGroup> groups = this.getMemberships(user);
        for (UserGroup group : groups) {
            this.deleteMembership(user, group);
        }
        Objectify ofy = ObjectifyService.begin();
        ofy.delete(user);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deleteUserGroup(UserGroup group) {
        List<Authorization> authorizations = authService
                .getAuthorizations(group);
        for (Authorization authorization : authorizations) {
            authService.deleteAuthorization(authorization);
        }
        List<User> users = getGroupMembers(group);
        for (User user : users) {
            deleteMembership(user, group);
        }
        Objectify ofy = ObjectifyService.begin();
        ofy.delete(group);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<User> getGroupMembers(UserGroup group) {
        return membershipDao.getMembers(group);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<UserGroup> getMemberships(User user) {
        return membershipDao.getGroups(user);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean setMembership(User user, UserGroup group) {
        return (membershipDao.save(user, group) != null);
    }

}
